package com.ruoyi.web.controller.teacher;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.BaseEqInfo;
import com.ruoyi.domain.BasisEquipmentInformationv;
import com.ruoyi.dto.BasisPositionEquipment;
import com.ruoyi.service.IBaseEqInfoService;
import com.ruoyi.service.IBasisEquipmentInformationvService;
import com.ruoyi.service.IBasisPositionService;
import com.ruoyi.service.IUserDataService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * 装备信息Controller
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@RestController
@RequestMapping("/system/informationv")
public class BasisEquipmentInformationvController extends BaseController
{
    @Autowired
    private IBasisEquipmentInformationvService basisEquipmentInformationvService;

    @Autowired
    private IBasisPositionService basisPositionService;

    @Autowired
    private IBaseEqInfoService baseEqInfoService;

    @Autowired
    private IUserDataService userDataService;

    /**
     * 查询装备信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(BasisEquipmentInformationv basisEquipmentInformationv)
    {
        startPage();
        List<BasisEquipmentInformationv> list = basisEquipmentInformationvService.selectBasisEquipmentInformationvList(basisEquipmentInformationv);
        return getDataTable(list);
    }


    @GetMapping("/findAllBasisEquipmentInformationv")
    public AjaxResult findAllBasisEquipmentInformationv()
    {
        List<BasisEquipmentInformationv> list = basisEquipmentInformationvService.selectBasisEquipmentInformationvList(new BasisEquipmentInformationv());
        return success(list);
    }

    /**
     * 导出装备信息列表
     */
    @Log(title = "装备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BasisEquipmentInformationv basisEquipmentInformationv)
    {
        List<BasisEquipmentInformationv> list = basisEquipmentInformationvService.selectBasisEquipmentInformationvList(basisEquipmentInformationv);
        ExcelUtil<BasisEquipmentInformationv> util = new ExcelUtil<BasisEquipmentInformationv>(BasisEquipmentInformationv.class);
        util.exportExcel(response, list, "装备信息数据");
    }

    /**
     * 获取装备信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(basisEquipmentInformationvService.selectBasisEquipmentInformationvById(id));
    }

    /**
     * 新增装备信息
     */
    @Log(title = "装备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BasisEquipmentInformationv basisEquipmentInformationv,@RequestBody BaseEqInfo baseEqInfo)
    {
        basisEquipmentInformationv.setType("天线");
        int ids = basisEquipmentInformationvService.insertBasisEquipmentInformationv(basisEquipmentInformationv);
        baseEqInfo.setEqid((long) basisEquipmentInformationv.getId());
        return toAjax(baseEqInfoService.insertBaseEqInfo(baseEqInfo));
    }

    /**
     * 修改装备信息
     */
    @Log(title = "装备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BasisEquipmentInformationv basisEquipmentInformationv,@RequestBody BaseEqInfo baseEqInfo)
    {
        basisEquipmentInformationv.setId(baseEqInfo.getEqid());
        baseEqInfoService.updateBaseEqInfo(baseEqInfo);
        int e = basisEquipmentInformationvService.updateBasisEquipmentInformationv(basisEquipmentInformationv);
        return toAjax(e);
    }

    /**
     * 删除装备信息
     */
    @Log(title = "装备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(basisEquipmentInformationvService.deleteBasisEquipmentInformationvByIds(ids));
    }
    @GetMapping("/queryType/{id}")
    public AjaxResult queryType (@PathVariable("id") Long id){
        return AjaxResult.success(basisEquipmentInformationvService.queryType(id));
    }





    @GetMapping("/findByEquipmentTypeEquipmentModel")
    public AjaxResult findByEquipmentTypeEquipmentModel (String equipmentType,String equipmentModel,String loadingId,Integer positionId){
        return AjaxResult.success(basisEquipmentInformationvService.findByEquipmentTypeEquipmentModel(equipmentType, equipmentModel,loadingId,positionId));
    }



    @Log(title = "装备信息", businessType = BusinessType.INSERT)
    @PostMapping("/addBasisPositionEquipment")
    public AjaxResult addBasisPositionEquipment(@RequestBody BasisPositionEquipment basisPositionEquipment)
    {
        return toAjax(basisEquipmentInformationvService.addBasisPositionEquipment(basisPositionEquipment));
    }

    //根据案例和阵地查询设备
    @GetMapping("/findByPositionIdType")
    public AjaxResult findByPositionIdType (String loadingId,Integer positionId){
        return AjaxResult.success(basisEquipmentInformationvService.findByPositionIdType(positionId, loadingId));
    }


    @GetMapping("/delBasisPositionEquipmentInformationvEqid")
    public AjaxResult delBasisPositionEquipmentInformationvEqid (String loadingId,Integer positionId,Integer eqId){
        boolean flag = false;

        flag = basisEquipmentInformationvService.delBasisPositionEquipmentInformationvEqid(loadingId, positionId, eqId);

        if (flag){
            flag =  userDataService.delbasisPositionEquipmentUser(loadingId,positionId,eqId);

            if (!flag) return AjaxResult.success(flag);
        }

        return AjaxResult.success(flag);
    }

    //查询人员下的设备
    @GetMapping("/findbyloadingIdpositionIduserId")
    public AjaxResult findbyloadingIdpositionIduserId (String loadingId,Integer positionId,Integer userId){
        return AjaxResult.success(basisEquipmentInformationvService.findbyloadingIdpositionIduserId(loadingId, positionId, userId));
    }

}
